package dda;

import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.util.Observable;

public class SorterThread extends Observable implements Runnable {
	private int threadNumber;
	private Comparable[] list;
	private Thread thread;
	

	public SorterThread(Comparable[] list, int threadNumber) {
		this.threadNumber = threadNumber;
		this.list = list;
	}

	public SorterThread() {

	}

	public int getThreadNumber() {
		return threadNumber;
	}

	@Override
	public void run() {
		try {
			list = sort(list);
		} catch (Exception e) {
			e.printStackTrace();
		}
		setChanged();
		notifyObservers(list);
		clearChanged();
	}

	public Comparable[] sort(Comparable[] list) throws MalformedURLException,
			RemoteException, NotBoundException {
		ISortFactory isf = (ISortFactory) Naming
				.lookup("//localhost:1099/sortfactory");
		ISorter is = isf.createSorter();
		Comparable[] result = is.sort(list);
		
		return result;

	}

	public Comparable[] getList() {
		return this.list;
	}

	public void start() {
		thread = new Thread(this);
		thread.start();
	}
}
